<template>
  <div class="nav-container">
    <el-menu mode="horizontal" router class="nav-menu" :default-active="activeMenu">
      <div class="menu-left">
        <el-menu-item index="/">首页</el-menu-item>
        <el-menu-item index="/pet">领养宠物</el-menu-item>
        <el-menu-item index="/tasks">任务中心</el-menu-item>
      </div>
      
      <div class="nav-right">
        <el-dropdown @command="handleCommand" class="user-dropdown">
          <span class="user-menu">
            <el-avatar :size="32" class="user-avatar">{{ username.charAt(0) }}</el-avatar>
            <span class="username">{{ username }}</span>
            <el-icon class="el-icon--right"><arrow-down /></el-icon>
          </span>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item command="profile">个人中心</el-dropdown-item>
              <el-dropdown-item command="logout">退出登录</el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
    </el-menu>
  </div>
</template>

<script setup>
import { computed } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { useUserStore } from '../stores/user'
import { ArrowDown } from '@element-plus/icons-vue'

const router = useRouter()
const route = useRoute()
const userStore = useUserStore()
const username = computed(() => userStore.user?.username || '用户')

// 计算当前高亮菜单
const activeMenu = computed(() => {
  // 只取第一级路径
  const path = route.path
  if (path.startsWith('/pet')) return '/pet'
  if (path.startsWith('/tasks')) return '/tasks'
  if (path.startsWith('/profile')) return 'profile'
  return '/'
})

const handleCommand = (command) => {
  if (command === 'logout') {
    userStore.logout()
    router.push('/login')
  } else if (command === 'profile') {
    router.push('/profile')
  }
}
</script>

<style scoped>
.nav-container {
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
  position: relative;
}

.nav-menu {
  padding: 0 20px;
  height: 60px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.menu-left {
  display: flex;
  align-items: center;
}

.nav-right {
  height: 60px;
  display: flex;
  align-items: center;
}

.user-dropdown {
  height: 100%;
  display: flex;
  align-items: center;
}

.user-menu {
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 0 4px;
  border-radius: 20px;
  transition: all 0.3s ease;
}

.user-menu:hover {
  background: var(--el-color-primary-light-9);
}

.user-avatar {
  background: var(--el-color-primary);
  color: white;
  font-weight: bold;
}

.username {
  font-size: 14px;
  color: var(--el-text-color-primary);
}

:deep(.el-menu.el-menu--horizontal) {
  border-bottom: none;
}

:deep(.el-menu-item) {
  height: 60px;
  line-height: 60px;
}

:deep(.el-dropdown) {
  outline: none;
}

:deep(.el-dropdown:focus) {
  outline: none;
}

:deep(.el-dropdown:focus-visible) {
  outline: none;
}

.user-menu:focus {
  outline: none;
}

.user-menu:focus-visible {
  outline: none;
}
</style>
